﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	[Journaling(JournalingMode.NoCommandData)]
	public class SelectWindow : IExternalCommand
	{
		public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = revit.Application.ActiveUIDocument;
			Application application = revit.Application.Application;
			if (!WinFormTools.IsAdministrator())
			{
				TaskDialog.Show("盈建科提示", "请以管理员身份运行，否则无法保存数据。");
			}
			Transaction transaction = new Transaction(activeUIDocument.Document, "selectWin");
			try
			{
				transaction.Start();
				ICollection<Element> allLevel = RevitTools.GetAllLevel(revit);
				string title = activeUIDocument.Document.Title;
				string pathName = activeUIDocument.Document.PathName;
				if (activeUIDocument.Document.PathName == "")
				{
					TaskDialog.Show("盈建科提示", "请先保存文件。");
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				if (!XmlInfo.FormIsExist(title, pathName))
				{
					new FormSel(revit, message, elements, allLevel, WinFormTools.GetForegroundWindow().ToString()).Show();
				}
				else
				{
					TaskDialog.Show("info", "已经存在");
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				TaskDialog.Show("ex", ex.ToString());
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
